package com.huhu.contact.data;

import com.huhu.contact.ResourceTable;
import com.huhu.contact.model.*;

import com.huhu.contact.utils.Utils;
import ohos.app.Context;

import java.util.*;

/**
 * The CategoryData, Generate the javaBean of categoryPage
 */
public class ContactData {

    private List<ContactBean> resultList = new ArrayList<>(); // 最终结果（包含分组的字母）
    private List<String> characterList = new ArrayList<>(); // 字母List
    private List<ContactBean> mContactBeans = new ArrayList<>(); // 字母List

    private ContactData() {
        mContactBeans = mDefaultContactBeans;
    }

    private final List<ContactBean> mDefaultContactBeans = new ArrayList<ContactBean>() {{
        add(new ContactBean("张三", "15265236985"));
        add(new ContactBean("李四", "12285238985"));
        add(new ContactBean("王五", "18265436685"));
        add(new ContactBean("齐三", "18223436685"));
        add(new ContactBean("娥路过", "18265436685"));
        add(new ContactBean("任哥哥", "16535436685"));
        add(new ContactBean("唐老鼠", "18256736685"));
        add(new ContactBean("杨士大夫", "14587436685"));
        add(new ContactBean("欧老", "18265444485"));
        add(new ContactBean("天山", "18524836685"));
        add(new ContactBean("阿婆", "18265123455"));
        add(new ContactBean("傻逼", "17678936685"));
        add(new ContactBean("邓小说", "18233436685"));
        add(new ContactBean("韩国", "12322336685"));
        add(new ContactBean("华试试", "111436685"));
        add(new ContactBean("金玫瑰", "133436685"));
        add(new ContactBean("康二", "182654444"));
        add(new ContactBean("览啥", "182313668223"));
    }};


    public void sortContactData() {
        List<ContactBean> mContactList = new ArrayList<>();
        Map<String, String> map = new HashMap<>();

        for (ContactBean contactBean : mContactBeans) {
            String pinyin = Utils.getPingYin(contactBean.getName());
            map.put(pinyin, contactBean.getName());
            contactBean.setNamepy(pinyin);
            mContactList.add(contactBean);
        }
        mContactList.sort(new ContactComparator());
        characterList = new ArrayList<>();
        resultList = new ArrayList<>();
        for (ContactBean contactBean : mContactList) {
            String namepy = contactBean.getNamepy();
            String character = (namepy.charAt(0) + "").toUpperCase(Locale.ENGLISH);
            if (!characterList.contains(character)) {
                if (character.hashCode() >= "A".hashCode() && character.hashCode() <= "Z".hashCode()) { // 是字母
                    characterList.add(character);
                    resultList.add(new ContactBean(character, ContactBean.ITEM_TYPE.ITEM_TYPE_CHARACTER.ordinal()));
                } else {
                    if (!characterList.contains("#")) {
                        characterList.add("#");
                        resultList.add(new ContactBean("#", ContactBean.ITEM_TYPE.ITEM_TYPE_CHARACTER.ordinal()));
                    }
                }
            }

            resultList.add(new ContactBean(contactBean.getName(), contactBean.getTelephone(), map.get(namepy), ContactBean.ITEM_TYPE.ITEM_TYPE_CONTACT.ordinal()));
        }
    }

    // Generate the javaBean of ContactData
    public static ContactData get() {
        return new ContactData();
    }

    public List<ContactBean> getDefaultContactBeans() {
        return mDefaultContactBeans;
    }

    public List<ContactBean> getContactBeans() {
        return mContactBeans;
    }

    public void addContactBean(String name, String phone) {
        mContactBeans.add(new ContactBean(name, phone));
    }

    public List<ContactBean> deleteContactBeans(ContactBean item) {
        mContactBeans.removeIf(contactBean -> contactBean.getName().equals(item.getName()));

        return mContactBeans;
    }

    public List<ContactBean> getResultList() {
        return resultList;
    }

    public List<String> getCharacterList() {
        return characterList;
    }

    public int getScrollPosition(String character) {
        if (characterList.contains(character)) {
            for (int i = 0; i < resultList.size(); i++) {
                if (resultList.get(i).getCharacter().equals(character)) {
                    return i;
                }
            }
        }

        return -1; // -1不会滑动
    }
}
